﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using e_commerce.BLL.Data;

namespace e_commerce.BLL.Services.Enterprise
{
    public class RoleManager:IDisposable
    {
        private commerceDbContext _dbContext;
        private AccountService _aService;

        public RoleManager()
        {
            _dbContext = new commerceDbContext();
            _aService = new AccountService();
        }

        public IEnumerable<Role> GetUserRoles(int userId)
        {
            var user = _aService.GetUserById(userId);
            return user.Role;
        }

        public IEnumerable<Role> GetRoles()
        {
            return _dbContext.Role;
        }

        public void CreateRole(string roleName,string code,ICollection<Permissions> permissions)
        {
            Role role = new Role()
            {
                RoleName = roleName,
                CreationTime = DateTime.Now,
                Permissions = permissions,
                Code = code,
            };
            _dbContext.Role.Add(role);
            SaveChange();
        }

        public void SaveChange()
        {
            _dbContext.SaveChanges();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }
}
